﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;

namespace SKN
{
    /// <summary>
    /// This class have methods to create test data in sharepoint environment
    /// </summary>
    public static class TestDataGenerator
    {
        /// <summary>
        /// Use this method to add items to a list
        /// </summary>
        /// <param name="web"></param>
        /// <param name="listName"></param>
        /// <param name="fields"></param>
        /// <param name="itemsToLoad"></param>
        public static void LoadListItems (SPWeb web, string listName, List<string> fields, int itemsToLoad)
        {
            SPList list = web.Lists[listName];
            for (int i = 1; i <= itemsToLoad; i++)
            {
                SPListItem item = list.Items.Add();
                foreach (string fieldName in fields)
                {
                    item[fieldName] = string.Format("{0} {1}", fieldName, i.ToString());
                }
                item.Update();
            }
            list.Update();
        }
        
        public static void LoadDiscussionList (SPList list, int discussionCount, int maxResponsesCount, bool cleanListFirst)
        {
            for (int i = 0; i < discussionCount; i++)
            {
                string discussionSubject = string.Format("This is the subject of Discussion {0}", i);
                SPListItem objDiscussion = SPUtility.CreateNewDiscussion(list.Items, discussionSubject);

                objDiscussion[SPBuiltInFieldId.Body] = "This is the Body of the Discussion";

                objDiscussion.Update();
              
                Random r = new Random();
                int randomResponseCount = r.Next(0, maxResponsesCount);
                for (int j = 0; j < randomResponseCount; j++)
                {
                    SPListItem reply = SPUtility.CreateNewDiscussionReply(objDiscussion);
                    reply["Body"] = string.Format("Discussion's reply {0}", j);
                    reply.Update();                   
                }
            }
        }

        public static void LoadGenericList (SPList list, int itemCount, bool cleanListFirst)
        {
            Random r = new Random();
            int randomNumber = 0;
            bool seedExist = false;

            seedExist = list.ItemCount > 0;

            SPFieldCollection fields = list.Fields;

            Dictionary<string, string> fieldsSelected = new Dictionary<string, string>();

            foreach (SPField field in fields)
            {
                if (!(field.Hidden || field.ReadOnlyField))
                    if (field.Type == SPFieldType.Text
                    || field.Type == SPFieldType.Note
                    || field.Type == SPFieldType.Number
                        || field.Type == SPFieldType.Boolean
                    || field.Type == SPFieldType.Choice
                        || field.Type == SPFieldType.DateTime)
                    {
                        fieldsSelected.Add(field.Title, field.TypeAsString);
                    }
            }

            // check to find if seed exist
            SPListItem listItemSeed = null;
            if (seedExist)
            {
                listItemSeed = list.Items[0];
            }

            // Logic to delete all the items from the list except one
            int itemsToDelete = list.ItemCount;
            if (cleanListFirst)
            {
                for (int j = itemsToDelete - 1; j > 0; j--)
                {
                    list.Items[j].Delete();
                }
            }

            // start creating test data
            for (int i = 0; i < itemCount; i++)
            {
                SPListItem item = list.Items.Add();

                foreach (KeyValuePair<string, string> fieldTitle in fieldsSelected)
                {
                    if (seedExist) // seed exist 
                    {
                        object value;

                        // check for null 
                        if (null != listItemSeed[fieldTitle.Key])
                        {
                            value = listItemSeed[fieldTitle.Key];

                            // if field is type=text , we append the counter 
                            switch (fieldTitle.Value)
                            {
                                case "Text":
                                    item[fieldTitle.Key] = string.Format("{0} {1}", value.ToString(), i);
                                    break;
                                case "Choice":
                                    SPFieldChoice spChoiceField = list.Fields[fieldTitle.Key] as SPFieldChoice;
                                    randomNumber = r.Next(0, spChoiceField.Choices.Count - 1);
                                    item[fieldTitle.Key] = spChoiceField.Choices[randomNumber];
                                    break;
                                case "Number":
                                    randomNumber = r.Next(0, 100);
                                    item[fieldTitle.Key] = randomNumber;
                                    break;
                                default:
                                    item[fieldTitle.Key] = value;
                                    break;
                            }
                        }
                    }
                    else // seed missing
                    {
                        item[fieldTitle.Key] = string.Format("{0} {1}", fieldTitle, i);
                    }
                }
                item.Update();
            }
            list.Update();
        }
    }
}
